<template>
    <div class="login-wrap">
        <div class="ms-login">
            <div class="ms-title">用户注册</div>
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="0px" class="ms-content">
                <el-form-item prop="username">
                    <el-input v-model="ruleForm.username" prefix-icon="el-icon-third-xingmingyonghumingnicheng" placeholder="账户名">
                        <!-- <el-button slot="prepend" icon="el-icon-third-xingmingyonghumingnicheng"></el-button> -->
                    </el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input type="password" prefix-icon="el-icon-third-mima" placeholder="密码" clearable v-model="ruleForm.password">
                            <!-- @keyup.enter.native="submitForm('ruleForm') -->
                        <!-- <el-button slot="prepend" icon="el-icon-third-mima"></el-button> -->
                    </el-input>
                </el-form-item>
                <el-form-item prop="spassword">
                    <el-input type="password" prefix-icon="el-icon-third-mima" placeholder="再一次输入密码" clearable v-model="ruleForm.spassword">
                        <!-- <el-button slot="prepend" icon="el-icon-third-mima"></el-button> -->
                    </el-input>
                </el-form-item>
                <el-form-item prop="email">
                    <el-input type="email" prefix-icon="el-icon-third-youxiang" placeholder="邮箱地址 " clearable v-model="ruleForm.email" @keyup.enter.native="submitForm('ruleForm')">
                        <!-- <el-button slot="prepend" icon="el-icon-third-mima"></el-button> -->
                    </el-input>
                </el-form-item>
                
                <div class="login-btn">
                    <el-button type="primary" icon="el-icon-third-zhuce" @click="submitForm('ruleForm')">注册</el-button>
                </div>
                <p class="login-tips"> 注意 : 两次密码需要一致。</p>
            </el-form>
        </div>
    </div>
</template>

<script>
    var AV = require('leancloud-storage');
     

    export default {
        data: function(){
            return {
                ruleForm: {
                    username: 'admin',
                    password: '123456',
                    spassword: '123456',
                    email:'172399463@qq.com'
                },
                rules: {
                    username: [
                        { required: true, message: '请输入用户名', trigger: 'blur' }
                    ],
                    password: [
                        { required: true, message: '请输入密码', trigger: 'blur' }
                    ],
                    spassword:[
                        {required:true, message:'请再次输入密码', trigger:'blur'}
                    ],
                    email:[
                        {required:true,message:'请输入邮箱地址',trigger:'blur'},
                        {type:'email',message:'请输入一个有效的邮箱地址',trigger: 'blur,change'}
                    ]
                }
            }
        },
        methods: {
            submitForm(formName) {                
                // debugger;
                if(this.ruleForm.password !== this.ruleForm.spassword){
                   this.ruleForm.password = this.ruleForm.spassword = ''
                   this.$message.error('输入密码不一致，请重新输入')
                }
                var that = this;

                this.$refs[formName].validate((valid) => {
                        if(valid)
                        {                            
                            var user = new AV.User();
                            user.setUsername(that.ruleForm.username);
                            user.setPassword(that.ruleForm.password);
                            user.setEmail(that.ruleForm.email);
                            user.signUp().then(function (loginedUser) {      
                                debugger;
                                return loginedUser;
                            }).then(function( loginedUser){
                                // 设置账户类型为后台账户
                                loginedUser.set('accountType','bg')
                                loginedUser.save().then(()=>{
                                    that.$message.info('注册成功')

                                    console.log(loginedUser)
                                    that.$router.push('/login')
                                })

                            }).catch(error=>{
                                // alert();
                                console.log(JSON.stringify(error))
                                that.$message.error(error.rawMessage)                            
                            });
                        }
                        
                            

                    // if (valid) {
                    //     localStorage.setItem('ms_username',this.ruleForm.username);
                    //     this.$router.push('/');
                    // } else {
                    //     console.log('error submit!!');
                    //     return false;
                    // }
                });
            }
        }
    }
</script>

<style scoped>
    .login-wrap{
        position: relative;
        width:100%;
        height:100%;
        /* background-image: url(../../assets/login-bg.jpg); */
        background: skyblue no-repeat;
        background-size: 100%;
    }
    .ms-title{
        width:100%;
        line-height: 50px;
        text-align: center;
        font-size:20px;
        /* color: #fff; */
        border-bottom: 1px solid #ddd;
    }
    .ms-login{
        position: absolute;
        left:50%;
        top:50%;
        width:350px;
        margin:-190px 0 0 -175px;
        border-radius: 5px;
        background: rgba(255,255,255, 0.3);
        overflow: hidden;
    }
    .ms-content{
        padding: 30px 30px;
    }
    .login-btn{
        text-align: center;
    }
    .login-btn button{
        width:100%;
        /* height:36px; */
        margin-bottom: 10px;
    }
    .login-tips{
        font-size:12px;
        line-height:30px;
        color:#fff;
    }
</style>